package com.beamfield.controller.api.mb;

import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.enums.MessageEnum;
import com.beamfield.model.AdminInfo;
import com.beamfield.model.ProcDoc;
import com.beamfield.service.manage.auth.AdminInfoService;
import com.beamfield.service.manage.process.ProcessService;
import com.beamfield.service.manage.sys.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenjj
 * @Description:生产管理
 * @Date 2018/10/16
 */
@Controller
@RequestMapping(value = "/mb_api/produce")
public class MbProduceController extends MbBaseController {
    private final ProcessService processService;
    private final MessageService messageService;
    private final AdminInfoService adminInfoService;

    @Autowired
    public MbProduceController(ProcessService processService, MessageService messageService, AdminInfoService adminInfoService){
        this.processService = processService;
        this.messageService = messageService;
        this.adminInfoService = adminInfoService;
    }
    /**
     * 获取在建梁数据
     * @return
     */
    @RequestMapping(value = "/getBeamProduceData", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getProduceData(HttpServletRequest request){
        Long beamId = super.getLogInfo(request).getBeamId();
        try{
            return processService.getMbBeamProduceData(beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }
    /**
     * 搜索在建梁
     * @return
     */
    @RequestMapping(value = "/getBeamProduceList", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getBeamProduceList(HttpServletRequest request){
        Long beamId = super.getLogInfo(request).getBeamId();

        JSONObject jsonObject = super.getBody(request);
        if(jsonObject == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Integer offset = jsonObject.getInteger("offset");
        Integer limit = jsonObject.getInteger("limit");
        String beamNo = jsonObject.getString("beamNo");
        if(StringUtils.isEmpty(beamNo)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return new ApiResult(processService.getMbBeamProduceList(offset,limit,beamNo,beamId));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取制梁通知单基本信息
     * @return
     */
    @RequestMapping(value = "/getProcNoticeDetail", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getProcNoticeDetail(HttpServletRequest request){
        Long beamId = super.getLogInfo(request).getBeamId();

        JSONObject jsonObject = super.getBody(request);
        if(jsonObject == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String noticeId = jsonObject.getString("noticeId");
        if(StringUtils.isEmpty(noticeId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return new ApiResult(processService.getMbProcNoticeDetail(noticeId,beamId));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取工序人员和审核历史
     * @return
     */
    @RequestMapping(value = "/getOperatorAndReview", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOperatorAndReview(HttpServletRequest request){
        Long beamId = super.getLogInfo(request).getBeamId();

        JSONObject jsonObject = super.getBody(request);
        if(jsonObject == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String noticeId = jsonObject.getString("noticeId");
        String step = jsonObject.getString("step");
        if(StringUtils.isEmpty(noticeId) || StringUtils.isEmpty(step)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return new ApiResult(processService.getMbOperatorAndReview(noticeId,null,step,beamId));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取文档列表
     * @return
     */
    @RequestMapping(value = "/getProcDoc", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getProcDoc(HttpServletRequest request){
        Long beamId = super.getLogInfo(request).getBeamId();

        JSONObject jsonObject = super.getBody(request);
        if(jsonObject == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String noticeId = jsonObject.getString("noticeId");
        String step = jsonObject.getString("step");
        if(StringUtils.isEmpty(noticeId) || StringUtils.isEmpty(step)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return new ApiResult(processService.getMbProcDoc(noticeId,step,beamId));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 保存通知单审核数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveProcessReview",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveProcessReview(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String jcx = body.getString("jcx");
        String pass = body.getString("pass");
        String type = body.getString("type");
        String noticeId = body.getString("noticeId");
        String step = body.getString("step");
        String remark = body.getString("remark");
        if(StringUtils.isEmpty(jcx)||StringUtils.isEmpty(pass)|| StringUtils.isEmpty(type)||StringUtils.isEmpty(noticeId)||StringUtils.isEmpty(step)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);

        try {
            switch (Integer.parseInt(step)) {
                //钢筋绑扎
                case 1 : return processService.saveProcessReview$GJBZ(jcx, noticeId, pass, remark, beamId, userId, type);
                //模板安装
                case 2 : return processService.saveProcessReview$MBAZ(jcx, noticeId, pass, remark, beamId, userId);
                //混凝土
                case 3 : return processService.saveProcessReview$HNT(jcx, noticeId, pass, remark, beamId, userId);
                //预张拉
                case 6 : return processService.saveProcessReview$YZL(jcx, noticeId, pass, remark, beamId, userId);
                //初张拉
                case 7 : return processService.saveProcessReview$CZL(jcx, noticeId, pass, remark, beamId, userId);
                //终拉张
                case 9 : return processService.saveProcessReview$ZZL(jcx, noticeId, pass, remark, beamId, userId);
                //切丝压浆
                case 10 : return processService.saveProcessReview$QSYJ(jcx, noticeId, pass, remark, beamId, userId, type);
                //封锚
                case 11 : return processService.saveProcessReview$FM(jcx, noticeId, pass, remark, beamId, userId, type);
                //防水
                case 12 : return processService.saveProcessReview$FS(jcx, noticeId, pass, remark, beamId, userId, type);
                //架梁
                case 13 : return processService.saveProcessReview$JL(jcx, noticeId, pass, remark, beamId, userId, type);
                default :return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 保存工序文档
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveDoc", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveDoc(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        ProcDoc procDoc = body.getObject("procDoc", ProcDoc.class);
        if(procDoc == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        AdminInfo adminInfo = (AdminInfo) adminInfoService.getLoginInfo(userId).getResult();
        procDoc.setRecorder(adminInfo.getEcode());
        try{
            return processService.saveProcessDoc(procDoc, beamId, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }
}
